<?php
namespace app\index\controller;

use think\Db;
use think\Controller;
use app\index\controller\RandName;

class TestDb extends Controller
{
    public function index($name="",$age="",$city="",$status="")
    {
        $out = '你好：'  . $name . ', 你的年龄是: ' . $age . ', 来自: ' . $city .' !';
        $randName = new RandName();
        $out = $randName->randName(10000);
        dump($out);
    }

    public function add($name="",$age="",$sex="",$city="",$password="",$level="")
    {
        $name = $name . '-' . rand(100,1000000);
        $age = $age ? $age : rand(10,100);

        $data = ['name'=>$name,'age'=>$age,'sex'=>$sex,'city'=>$city,'password'=>$password,'level'=>$level];

        $res = Db::name('user')->insert($data);
        dump($res);
    }

    public function createUser($num=100)
    {
        //随机生成用户
        $rn = new RandName();
        $users = $rn->randName($num);
        $count=0;

        //入库
        foreach ($users as $name)
        {

            $age = rand(1,100);
            $data = ['name'=>$name,'age'=>$age];
            $res = Db::name('user')->insert($data);
            $count++;
        }
        $out = '成功生成 "'  . $count . '" 位用户。';
        dump($out);

    }
}
